package com.raycloud.dingtalk.starter.context;

import com.raycloud.dingtalk.starter.annotation.Command;
import com.raycloud.dingtalk.starter.handle.CommandHandle;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.Map;

public class CommandListener implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        ApplicationContext context = contextRefreshedEvent.getApplicationContext();
        Map<String, Object> beanMaps = context.getBeansWithAnnotation(Command.class);

        for (Map.Entry<String, Object> entry : beanMaps.entrySet()) {
            Object bean = entry.getValue();
            Command command = bean.getClass().getAnnotation(Command.class);
            CommandContext.register(command.value(), command, (CommandHandle) bean, command.options());
        }


    }
}
